#include <54func.h>
int main(int argc,char *argv[])
{
    // ./8_block_select 1.pipe      -->  对一个文件进行读写，实现能读就读，能写就写
    ARGS_CHECK(argc,2);
    int fd1 = open(argv[1],O_RDWR);
    int fd2 = open(argv[1],O_RDWR);
    ERROR_CHECK(fd1,-1,"open read");
    ERROR_CHECK(fd2,-1,"open write");

    fd_set rdset;//读就绪集合
    fd_set wrset;//写就绪集合
    int cnt = 0;
    char buf[4096] = {0};
    //在这里，默认fd1读文件  fd2写文件
    while(1){
        //设置监听集合
        FD_ZERO(&rdset);
        FD_ZERO(&wrset);
        //设置好监听集合
        FD_SET(fd1,&rdset);
        FD_SET(fd2,&wrset);
        //os监听轮询
        int ret = select(fd2+1,&rdset,&wrset,NULL,NULL);
        if(FD_ISSET(fd1,&rdset)){
            //memset(buf,0,sizeof(buf));
            printf("read ready,cnt = %d,ret = %d\n",cnt++,ret);
            read(fd1,buf,2048);
        }else if(FD_ISSET(fd2,&wrset)){
            //memset(buf,0,sizeof(buf));
            printf("write ready,cnt = %d,ret = %d\n",cnt++,ret);
            write(fd2,buf,4096);
        }
        sleep(1);
    }
        close(fd1);
        close(fd2);

    return 0;
}
